package com.example.demo.common.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.entity.Inclusion;
import com.example.demo.common.mapper.InclusionMapper;
import com.example.demo.common.service.InclusionService;
import com.example.demo.common.utils.Compress;
import com.example.demo.common.utils.IpUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mixed")
public class InclusionController {
    @Resource
    Compress compress;
    @Resource
    InclusionService inclusionService;
    @Resource
    InclusionMapper inclusionMapper;
    @Resource
    IpUtil ipUtil;
    @PostMapping
    public Result<?> save(@RequestBody Inclusion inclusion) throws IOException {
        System.out.println(inclusion);
        inclusionService.saveInclusion(inclusion);
        inclusionService.updateInclusion(getZIP(inclusion));
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody Inclusion inclusion){
        inclusionService.updateInclusion(inclusion);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){
        inclusionService.deleteById(id);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Inclusion> wrapper = Wrappers.<Inclusion>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Inclusion::getRef, search);
        }
        Page<Inclusion> InclusionPage = inclusionMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(InclusionPage);
    }
    public Inclusion replceIP(Inclusion inclusion){
        if (inclusion.getAngleImg() !=null&& !inclusion.getAngleImg().equals("")){
            inclusion.setAngleImg(inclusion.getAngleImg().replace("http://"+ipUtil.getIp()+"/1/",""));
        }
        if (inclusion.getAztecsteel() !=null&& !inclusion.getAztecsteel().equals("")){
            inclusion.setAztecsteel(inclusion.getAztecsteel().replace("http://"+ipUtil.getIp()+"/1/",""));
        }
        if (inclusion.getAztecreport() !=null&& !inclusion.getAztecreport().equals("")){
            inclusion.setAztecreport(inclusion.getAztecreport().replace("http://"+ipUtil.getIp()+"/1/",""));
        }
        return inclusion;
    }
    public Inclusion getZIP(Inclusion inclusion) throws IOException {
        List<String> sourceFilePaths = new ArrayList<String>();
        sourceFilePaths.add("E:\\test\\1\\"+inclusion.getAztecreport());
        sourceFilePaths.add("E:\\test\\1\\"+inclusion.getAztecsteel());
        sourceFilePaths.add("E:\\test\\1\\"+inclusion.getAngleImg());
        String zipTempFilePath = "E:/test/1/inclusion/"+inclusion.getId()+'-'+inclusion.getRef()+".zip";
        int s = compress.compress1(sourceFilePaths, zipTempFilePath,false);
        inclusion.setZip("http://10.39.199.216/1/inclusion/"+inclusion.getId()+'-'+inclusion.getRef()+".zip");
        return inclusion;
    }
}


